<template>
	<view>
		<block v-for="(item, index) in list" :key="index">
			<view @click="jump(item)">
				<view class="post-item">
					<view class="post-item-top-user">
						<view @click.stop="toUcenter(item.user_id)">
							<u-avatar class="avatar" :customStyle="{ marginRight: '20rpx' }"
								:src="$utils.imgPath(item.user.avatar)"></u-avatar>
						</view>
						<view class="center">
							<view style="display: flex; align-items: center">
								<text class="username" v-if="item.user">{{ item.user.nickname.substring(0, 12) }}</text>
							</view>
							<view>
								<text class="time">{{ item.created_at | timeFrom }}</text>
								<text class="time" style="margin-left: 20rpx">发布于【{{ item.topic.name }}】</text>
							</view>
						</view>
						<view class="right">
							<u-icon :size="20" @tap.stop="onActive(item, index)" class="arrow-down"
								name="more-dot-fill"></u-icon>
						</view>
					</view>
					<view class="post-content">
						<view class="post-content-wrap u-line-5">
							<navigator class="discuss-item" :url="'/pages/discuss/detail?title=' + item2"
								v-for="(item2, index2) in item.discuss_list" :key="index">
								#{{ item2 }}#
							</navigator>
							<text>{{ item.content }}</text>
						</view>
						<!-- 帖子类型 -->
						<!--一张图片-->
						<block v-if="item.media.length == 1">
							<image :lazy-load="true" mode="aspectFill" class="img-style-1 cover-img"
								:src="$utils.imgPath(item.media[0])"
								@tap.stop="previewImage(item.media[0], item.media)"></image>
						</block>
						<!--二张图片-->
						<block v-if="item.media.length == 2">
							<view class="img-style-2">
								<image :lazy-load="true" class="cover-img" v-for="(mediaItem, index2) in item.media"
									:key="index2" @tap.stop="previewImage(mediaItem, item.media)" mode="aspectFill"
									:src="$utils.imgPath(mediaItem)"></image>
							</view>
						</block>
						<!--三张以上图片-->
						<block v-if="item.media.length > 2">
							<view class="img-style-3">
								<image class="cover-img" :lazy-load="true" v-for="(mediaItem, index2) in item.media"
									:key="index2" @tap.stop="previewImage(mediaItem, item.media)" mode="aspectFill"
									:src="$utils.imgPath(mediaItem)"></image>
							</view>
						</block>
					</view>
					<!-- 话题 -->
					<navigator :url="'/pages/discuss/detail?id='+item.discuss.id" v-if="item.discuss"
						style="font-size: 28rpx;color:#465cff;margin-left: 100rpx;margin-top: 10rpx;">
						<text>#{{ item.discuss.title }}#</text>
					</navigator>
					<!-- 位置 -->
					<view class="address" v-if="item.address">
						<u-icon name="map-fill" size="25rpx"></u-icon>
						<text class="text">{{ item.address }}</text>
					</view>
					<!-- 底部 -->
					<view class="p-footer">
						<view class="p-item">
							<u-icon name="eye"></u-icon>
							<text class="count">{{ item.see_num }}</text>
						</view>
						<view class="p-item margin50">
							<u-icon name="chat"></u-icon>
							<text class="count">{{ item.comment_num }}</text>
						</view>
						<view v-show="item.is_thumb" class="p-item" @click.stop="cancelCollection(item.id, index)">
							<u-icon name="heart-fill" color="#cc0000"></u-icon>
							<text class="count">{{ item.thumb_num }}</text>
						</view>
						<view v-show="!item.is_thumb" class="p-item" @click.stop="addCollection(item.id, index)">
							<u-icon name="heart"></u-icon>
							<text class="count">{{ item.thumb_num }}</text>
						</view>
					</view>
				</view>
			</view>
		</block>
		<!-- 操作弹窗 -->
		<u-action-sheet :actions="actionList" :show="showAction" @select="actionCon" cancelText="取消"
			@close="showAction = false"></u-action-sheet>
		<!-- 加载状态 -->
		<block v-if="list.length === 0 && loadStatus == 'nomore'">
			<u-empty :text="tips ? tips : '暂无内容'" icon="/static/empty.png"></u-empty>
		</block>
		<block v-else>
			<view style="margin: 30rpx 0">
				<u-loadmore :status="loadStatus" loadingIcon="semicircle" :iconColor="themesColor" />
			</view>
		</block>
	</view>
</template>

<script>
	import {
		themesColor
	} from '@/uni.scss';
	export default {
		name: 'q-post-list',
		props: {
			list: Array,
			loadStatus: String,
			tips: String,
			topic: Object
		},
		data() {
			return {
				themesColor,
				showAction: false,
				actionList: [],
				choosePost: '',
				chooseIndex: ''
			};
		},
		computed: {
			userInfo() {
				return this.$store.state.userInfo;
			}
		},
		methods: {
			imgResize(url) {
				return this.$c.fileUrl + '/' + url + '?x-oss-process=image/resize,w_500,m_lfit';
			},
			onActive(postInfo, index) {
				this.showAction = true;
				this.choosePost = postInfo;
				this.chooseIndex = index;

				this.actionList = [{
					name: '关注TA',
					key: 'follow'
				}];

				//如果是圈主或管理员
				if (this.topic && this.topic.is_admin) {
					this.actionList.unshift({
						name: '置顶',
						key: 'top'
					});

					this.actionList.push({
						name: '删除',
						color: 'red',
						key: 'delete'
					});
				} else if (this.userInfo.id == postInfo.user_id) {
					//自己发布的帖子可以删除
					this.actionList.push({
						name: '删除',
						color: 'red',
						key: 'delete'
					});
				}
			},
			actionCon(e) {
				let key = e.key;
				if (key == 'follow') {
					this.follow();
				}

				if (key == 'delete') {
					this.postDel();
				}

				if (key == 'top') {
					this.postTop();
				}
			},
			cancelCollection(id, index) {
				this.$H
					.post('post/thumbCancel', {
						id: id
					})
					.then((res) => {
						if (res.code === 1) {
							this.list[index].is_collection = false;
							this.list[index].collection_count--;
						}
					});
			},
			addCollection(id, index) {
				this.$H
					.post('post/thumbAdd', {
						id: id,
						user_id: this.list[index].user_id
					})
					.then((res) => {
						if (res.code === 1) {
							this.list[index].is_collection = true;
							this.list[index].collection_count++;
						}
					});
			},
			// 置顶帖子
			postTop() {
				this.$H
					.post('post/setTop', {
						post_id: this.choosePost.id,
						topic_id: this.choosePost.topic_id
					})
					.then((res) => {
						if (res.code == 200) {
							this.$q.toast('成功设为置顶', 'success');
						}
					});
			},
			postDel() {
				this.$H
					.post('post/del', {
						id: this.choosePost.id
					})
					.then((res) => {
						if (res.code == 200) {
							this.list.splice(this.chooseIndex, 1);
						}
					});
			},
			follow() {
				this.$H
					.post('user/addFollow', {
						user_id: this.choosePost.user_id
					})
					.then((res) => {
						this.$u.toast(res.msg);
					});
			},
			previewImage(url, urls) {
				let imgList = [];
				urls.forEach((item) => {
					imgList.push(this.$utils.imgPath(item, false));
				});

				uni.previewImage({
					current: this.$utils.imgPath(url, false),
					urls: imgList
				});
			},
			jump(e) {
				uni.navigateTo({
					url: '/pages/post/detail?id=' + e.id
				});
			},
			toDiscuss(e) {
				uni.navigateTo({
					url: '/pages/discuss/detail?id=' + e.currentTarget.dataset.id
				});
			},
			toUcenter(userId) {
				uni.navigateTo({
					url: '/pages/user/home?user_id=' + userId
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.post-item {
		background: #fff;
		padding: 20rpx;
		border-bottom: 1px solid #f5f5f5;

		.post-content {
			margin-top: 20rpx;
			margin-left: 100rpx;

			.cover-img {
				height: 400rpx;
			}

			.img-style-1 {
				display: block;
				width: 50%;
				max-height: 600rpx;
				gap: 10rpx;
				overflow: hidden;
			}

			.img-style-2 {
				display: flex;
				gap: 10rpx;
			}

			.img-style-3 {
				display: flex;
				flex-wrap: wrap;

				.cover-img {
					width: 31.3%;
					height: 200rpx;
					margin: 1%;
					border-radius: 5px;
				}
			}
		}

		.address {
			display: inline-flex;
			font-size: 20rpx;
			background-color: #f5f5f5;
			border-radius: 20rpx;
			padding: 5rpx 20rpx;
			margin: 20rpx 0;
			margin-left: 100rpx;

			.text {
				margin-left: 5rpx;
			}
		}
	}

	.post-item-top-user {
		display: flex;
		align-items: center;

		.avatar {
			width: 85rpx;
			height: 85rpx;
			border-radius: 50%;
			margin-right: 20rpx;
		}

		.center {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: 24rpx;
			color: #999;

			.username {
				font-size: 32rpx;
				font-weight: 600;
				color: #616161;
			}

			.official {
				display: inline-block;
				font-size: 20rpx;
				color: #fff;
				background-color: $themes-color;
				padding: 2rpx 10rpx;
				border-radius: 10rpx;
				margin-left: 10rpx;
			}
		}

		.right {
			height: 85rpx;

			.arrow-down {
				color: #999;
			}
		}
	}

	.p-footer {
		display: flex;
		margin: 20rpx 0;

		.p-item {
			margin: 0 auto;
			color: #616161;
			display: flex;
			align-items: center;

			.count {
				margin-left: 10rpx;
				font-size: 28rpx;
			}
		}

		.p-item[hidden] {
			display: none !important;
		}
	}

	.comment-wrap {
		font-size: 28rpx;
		padding-top: 20rpx;
		border-top: 1px solid #f5f5f5;

		.item {
			.name {
				color: #000;
				font-weight: 600;
			}
		}
	}

	.post-content-wrap {
		.discuss-item {
			display: inline-block;
			color: #2b85e4;
			margin-right: 20rpx;
		}
	}

	.cover-img {
		background-color: #f5f5f5;
		border-radius: 10rpx;
	}
</style>